#!/bin/sh
#
#   Copyright (c) International Business Machines  Corp., 2003, 2005
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#
#
#  FILE   : ftp
#
#  PURPOSE: Tests to see if ftp rejects a 'root' login attempt.
#
#  SETUP: The program `/usr/bin/expect' MUST be installed.
#
#  HISTORY:
#   03/04/03 Jerone Young (jeroney@us.ibm.com)
#   09/21/05 Kris Wilson (krisw@us.ibm.com) Check if vsftpd is on system.

#-----------------------------------------------------------------------
# FUNCTION:  do_setup
#-----------------------------------------------------------------------

do_setup()
{

    serv=vsftpd
    tvar=${MACHTYPE%-*}
    tvar=${tvar#*-}

    RHOST=${RHOST}
    TEST_USER=root
    FTPUSER_CONF="/etc/vsftpd/ftpusers"

    if [ -n "$PASSWORD" ]; then
        TEST_USER_PASSWD=$PASSWORD
    else
        end_testcase "You must set your password via the PASSWD variable."
    fi

    exists awk expect ftp vsftpd
    EchoResult "check commands"

    #if [ $tvar = "redhat" -o $tvar = "redhat-linux" -o $tvar = "koji-linux" ]; then
        echo "Verifying test user $TEST_USER is in /etc/vsftpd.ftpusers database..."
        FTPUSERS=$(${RSH} -l root -n $RHOST "awk '/$TEST_USER/' $FTPUSER_CONF" )
    #fi

    if [ -z "$FTPUSERS" ] ; then
        end_testcase "Test user $TEST_USER not found in /etc/ftpusers unable to run TEST, exiting 0 ..."
    fi

    if ! ${RSH} -l root -n $RHOST "command -v systemctl && /bin/systemctl restart ${serv}.service || /sbin/service ${serv} restart"; then
        end_testcase "Could not start vsftpd service on $RHOST."
    fi

    return 0

}

#-----------------------------------------------------------------------
# FUNCTION:  do_test
#-----------------------------------------------------------------------
do_test()
{

    echo "TINFO | Ftp should reject $TEST_USER from loging in successfully"
    TEST="ftp04"

    expect -c "
        spawn ftp $RHOST
        sleep 1
        expect -re \": \"
        send \"$TEST_USER\r\"
        expect -re \"Password:\"
        send \"$TEST_USER_PASSWD\r\"
        expect {
            # 230 - Login successful
            \"230\" {send_user \"==> TEST \#$TEST : FAIL (ftp allowed login attempt)\n\";exit 1}
            # 530 - Login failed
            \"530\" {send_user \"==> TEST \#$TEST : PASS (ftp rejected login attempt)\n\";exit 0}
        }
        expect \"ftp> \"
        send \"quit\r\"
    "

    [ $? -eq 0 ] || end_testcase "Testcase failed."

}

#----------------------------------------------------------------------
# FUNCTION: MAIN
#----------------------------------------------------------------------
# include lib files
if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
. ${SFROOT}/lib/XmlParse.sh

. ${CMDLIB}/lib/net_cmdlib.sh
. ${CMDLIB}/setup.sh

# get RHOST, RUSERNAME, PASSWORD from config.xml
DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

export RHOST=${REHOST}
export RUSERNAME=${RUSERNAME}
export PASSWORD=${PASSWORD}


read_opts $*
setup_env || { echo "setup env failed, test end."; exit 1; }

do_setup
do_test
EchoResult "ftp04 testcase."
